package com.wg.dome.handle;

import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.annotation.Request;
import com.wg.dome.entity.config.EquipJingLianEntity;
import com.wg.dome.entity.user.EquipEntity;
import com.wg.dome.entity.user.ItemEntity;
import com.wg.dome.entity.user.PlayerEntity;
import com.wg.dome.operation.JingLianRequest;
import com.wg.dome.operation.EquipJingLianResponse;
import com.wg.dome.operation.OperationCode;
import com.wg.dome.repository.EquipJingLianRepository;
import com.wg.dome.repository.EquipRepository;
import com.wg.dome.repository.ItemRepository;
import com.wg.dome.repository.PlayerRepository;
import com.wg.common.net.OperationRequest;
import com.wg.common.net.OperationResponse;
import com.wg.common.net.SendParameters;

/**
 * 精炼装备
 */
@Request(OperationCode = OperationCode.JINGLIAN)
public class HandleJingLianRequest extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {

        JingLianRequest jingLianRequest = new JingLianRequest(request);

        if (!jingLianRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(jingLianRequest);
        }

        if (jingLianRequest.getPlayerId() != peer.getPlayerId()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.PLAYERID_MATCH_ERROR, jingLianRequest.getPlayerId(), peer.getPlayerId());
        }

        PlayerRepository playerRepository = new PlayerRepository();
        PlayerEntity playerEntity = playerRepository.get(jingLianRequest.getPlayerId());
        long gold = playerEntity.getGold();


        EquipRepository equipRepository = new EquipRepository();
        EquipEntity equipEntity = equipRepository.getEntity(jingLianRequest.getPlayerId(), jingLianRequest.getEquipId());

        if (equipEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.EQUIP_ERROR, jingLianRequest.getEquipId());
        }

        short jlLv = equipEntity.getJlLv();

        EquipJingLianRepository equipJingLianRepository = new EquipJingLianRepository();
        EquipJingLianEntity jingLianentity = equipJingLianRepository.getNextEntity(jlLv);
        if (jingLianentity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.LEVEL_MAX);
        }
        if (jingLianentity.getUseGold() > gold) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.GOLD_LIMIT);
        }

        ItemRepository itemRepository = new ItemRepository();
        ItemEntity item = itemRepository.getItem(jingLianRequest.getPlayerId(), jingLianentity.getUseItemId());
        if (item == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.ITEM_LIMIT);
        }

        if (jingLianentity.getUseItemCount() > item.getCount()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.ITEM_LIMIT);
        }

        long willUseGold = 0;
        long useGold = 0;
        int willUseCount = 0;
        int useCount = 0;


        for (short i = equipEntity.getJlLv(); i < equipEntity.getJlLv() + 5; i++) {
            EquipJingLianEntity entity = equipJingLianRepository.getNextEntity(i);
            if (entity == null){
                break;
            }
            willUseGold += entity.getUseGold();
            willUseCount += entity.getUseItemCount();
            if (willUseGold > gold || willUseCount > item.getCount()) {
                break;
            }
            useGold += entity.getUseGold();
            useCount += entity.getUseItemCount();
            jlLv++;
        }


        playerEntity.setGold(gold - useGold);
        playerRepository.update(playerEntity);

        equipEntity.setJlLv(jlLv);
        equipRepository.update(equipEntity);

        item.setCount(item.getCount() - useCount);
        itemRepository.update(item);

        EquipJingLianResponse response = new EquipJingLianResponse();
        response.setEquipId(jingLianRequest.getEquipId());
        response.setCount(item.getCount());
        response.setLevel(jlLv);
        response.setGold(playerEntity.getGold());
        response.setItemId(item.getsId());

        return new OperationResponse(request.getOperationCode(), response);
    }
}
